<script setup lang='ts'>
  import { onMounted, onUpdated, reactive, ref } from 'vue';
  import { luckDrawApi } from '@/api/controller';
  import { $router, ToastSuccess, Toastloading } from '@/utils/public';
  import { getDateArray, getPlayEndTime, getSummaries } from '@/utils'
  import { ElMessageBox } from 'element-plus';
  import whiteForm from './whiteForm.vue';
  import table2excel from 'js-table2excel';
  import xmDatePicker from '@/components/xmDatePicker/index.vue';
  const whiteFormRef = ref()
  const action = reactive({
    list:[] as any[],
    timeObj:getPlayEndTime(getDateArray('ymdhis'))
  })
  const options = [
    {type:0,label:'否'},
    {type:1,label:'是'}
  ]
  const initialQuery = {
    user_id:'',
    gift_id:'',
    uuid:'',
    status:0,
    pool_level:0
  }
  const listQuery = reactive({
    ...initialQuery
  })
  const handleSerach = () => {
    getList();
  };
  const removeQuery = ()=>{
    Object.assign(listQuery,{
    ...initialQuery,
    pool_level:listQuery.pool_level
    })
  }
  const handleDownload = async()=>{
    const loading = Toastloading('数据导出中')
    const { playTime:date_st, endTime:date_ed } = action.timeObj
    const res = await luckDrawApi.wishDefenderWhiteList({
      ...listQuery,
      date_st,
      date_ed
    })
    loading.close()
    const { data=[] } = res
    const tHeader = [
      { title: '白名单ID', key: "id"},
      { title: '用户ID', key: "user_id"},
      { title: '用户UUID', key: "uuid"},
      { title: '用户昵称', key: "nickname"},
      { title: '礼物ID', key: "gift_id"},
      { title: '礼物名称', key: "gift_name"},
      { title: '礼物价格', key: "gift_price"},
      { title: '是否发放', key: "status_text"},
    ]
    const list = data.map((item:any)=>{
      return {
        status_text:options.find(res=>res.type == item.status)?.label || res.status,
        ...item
      }
    })
    table2excel(
      tHeader,
      list,
      `许愿转盘-礼物池白名单`)
  }
  const handleDel = (row:any = {})=>{
    const { nickname='', id=0 } = row
    ElMessageBox.confirm(`确定删除礼物池：${nickname}白名单吗?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
    .then(async () => {
      const loading = Toastloading();
      await luckDrawApi.wishDefenderDelPoolWhite({
        white_id:id,
      });
      loading.close();
      ToastSuccess('删除成功');
      getList();
    })
    .catch(() => {});
  }
  const handleCancel = (row:any = {})=>{
    const { nickname='', id=0 } = row
    ElMessageBox.confirm(`确定取消投放礼物池：${nickname}白名单吗?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
    .then(async () => {
      const loading = Toastloading();
      await luckDrawApi.wishDefenderCancelPoolWhite({
        white_id:id,
      });
      loading.close();
      ToastSuccess('取消投放成功');
      getList();
    })
    .catch(() => {});
  }
  const getList = async()=> {
    const loading = Toastloading()
    const { playTime:date_st, endTime:date_ed } = action.timeObj
    const res = await luckDrawApi.wishDefenderPoolWhiteList({
      ...listQuery,
      date_st,
      date_ed
    })
    loading.close()
    const { data=[] } = res
    action.list = data
  }
  let primarySet = false
  const getPoolLevel = async()=>{
    if(primarySet) return
    primarySet = true
    const { pool_level=0 } = $router().currentRoute.value?.meta || { pool_level:0 }
    if(listQuery.pool_level != pool_level && pool_level) {
      listQuery.pool_level = pool_level as number
      const loading = Toastloading()
      action.list = []
      removeQuery()
      await getList()
      primarySet = false
      loading.close()
    }
  }
  const updatePoolLevel = ()=>{
    primarySet = false
    getPoolLevel()
  }
  onMounted(()=>{
    updatePoolLevel()
  })
  onUpdated(()=>{
    updatePoolLevel()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">用户UUID：</div>
          <el-input v-model="listQuery.uuid" placeholder="请输入用户UUID"/>
        </div>
        <div class="labelItem">
          <div class="label">用户ID：</div>
          <el-input v-model="listQuery.user_id" placeholder="请输入用户ID"/>
        </div>
        <div class="labelItem">
          <div class="label">礼物ID：</div>
          <el-input v-model="listQuery.gift_id" placeholder="请输入礼物ID"/>
        </div>
        <div class="labelItem">
          <div class="label">是否发放：</div>
          <el-select v-model="listQuery.status" placeholder="请选择礼物池状态">
            <el-option label="全部状态" :value="-1"/>
            <el-option
              v-for="(item, index) in options"
              :key="index"
              :label="item.label"
              :value="item.type"
            />
          </el-select>
        </div>
        <xmDatePicker v-model:timeval="action.timeObj" />
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">搜索</el-button>
          <el-button type="primary" @click="whiteFormRef.openDialog({pool_level:listQuery.pool_level})">新增白名单</el-button>
          <el-button type="warning" @click="handleDownload">导出</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
      show-summary
      :summary-method="((param:any)=>getSummaries(param,[0,1,2,3,4,5,7]))"
    >
      <el-table-column width="100" prop="id" label="白名单ID" />
      <el-table-column min-width="80" prop="user_id" label="用户ID" />
      <el-table-column min-width="80" prop="uuid" label="用户UUID" />
      <el-table-column min-width="120" show-overflow-tooltip prop="nickname" label="用户昵称" />
      <el-table-column min-width="80" prop="gift_id" label="礼物ID" />
      <el-table-column min-width="120" show-overflow-tooltip prop="gift_name" label="礼物名称" />
      <el-table-column min-width="80" prop="gift_price" label="礼物价格" />
      <el-table-column min-width="80" prop="status" label="是否发放">
        <template #default="scoped">
          {{ options.find(res=>res.type == scoped.row.status)?.label || scoped.row.status }}
        </template>
      </el-table-column>
      <el-table-column width="120" fixed="right" label="操作">
        <template #default="scoped">
          <el-button v-if="!scoped.row.status" type="warning" @click="handleDel(scoped.row)">取消投放</el-button>
          <el-button v-if="scoped.row.status && (scoped.row.cur_admin_id == 18)" type="danger" @click="handleDel(scoped.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <whiteForm ref="whiteFormRef" @update-list="getList" />
  </div>
</template>
